package cn.zzuisa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import cn.zzuisa.entity.Config;
import cn.zzuisa.entity.Invitation;
import cn.zzuisa.entity.Member;
import cn.zzuisa.entity.Reply;
import cn.zzuisa.mapper.InvitationMapper;
import cn.zzuisa.service.ConfigService;
import cn.zzuisa.service.InvitationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.zzuisa.service.MemberService;
import cn.zzuisa.service.ReplyService;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author frank
 * @since 2019-05-07
 */
@Service
public class InvitationServiceImpl extends ServiceImpl<InvitationMapper, Invitation> implements InvitationService {


    @Autowired
    ReplyService replyService;

    @Autowired
    MemberService memberService;

    @Autowired
    ConfigService configService;
    @Override
    public IPage<Map<String, Object>> pageByEntity(IPage<Map<String, Object>> page, Invitation invitation) {
        IPage<Map<String, Object>> mapIPage = baseMapper.pageByEntity(page, invitation);
        Config one1 = configService.getOne(new QueryWrapper<Config>().eq("config_name", "hot_num"));
        String value = one1.getConfigValue();
        mapIPage.getRecords().forEach(item -> {
            int count = replyService.count(new QueryWrapper<Reply>().eq("invitation_id", item.get("id")));
            item.put("hotNum",value);
            item.put("replyNum",count);
            // 等级名称
            System.out.println(item);
            item.put("levelName",configService.getOne(new QueryWrapper<Config>().eq("config_name",item.get("level"))).getConfigValue());

            Reply one = replyService.getOne(new QueryWrapper<Reply>().eq("invitation_id", item.get("id")));
            if(one != null) {
                Member byId = memberService.getById(one.getMemberId());
                item.put("lastReplyName",byId.getName());
                item.put("lastReplyTime",one.getReplyTime());
            }else {
                item.put("lastReplyName","无");
                item.put("lastReplyTime",item.get("publish_time"));
            }
        });
        return mapIPage;
    }

    @Override
    public Map<String, Object> showInvitationById(Long id) {
        Map<String, Object> item = baseMapper.showInvitationById(id);
        item.put("levelName",configService.getOne(new QueryWrapper<Config>().eq("config_name",item.get("level"))).getConfigValue());
        return item;
    }
}
